import sys
import math

dic_file = sys.argv[1]
phi_file = sys.argv[2]
dim = int(sys.argv[3])

dics = {}
input_f = open(dic_file,'r')
c = 0
for lines in input_f:
    w = lines.strip().split('\t')
    dics[c] = w[-1]
    c = c + 1
input_f.close()

topics = []
input_f = open(phi_file,'r')
for lines in input_f:
    w = lines.strip().split('\t')
    word_id = int(w[0])
    s = w[1].split(' ')
    topic_id = int(s[dim].split(':')[0])
    value = float(s[dim].split(':')[1])
    topics.append((value,word_id))
input_f.close()

topics.sort()

buffer = [(word_id,value) for value,word_id in topics]
buffer.reverse()
s = str(topic_id)+'\n'
for i in range(0,50):
    s = s + dics[buffer[i][0]] + ' '
print s
